package com.study.d019fxml;

import java.net.URL;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**FXML的使用
*@description
*@author xtayhame
*date 2020年3月16日
*version
*since v1.0
*/
public class HelloFxmlMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		//第一种：编码的方式
		/*AnchorPane anchorPane = new AnchorPane();
		Button b1= new Button("Hello");
		b1.setPreWidth(40);
		b1.setPrefHeight(40);
		anchorPane.getChildren().add(b1);
		AnchorPane.setTopAnchor(b1, 20.0);
		AnchorPane.setLeftAnchor(b1, 20.0);*/


		//第二种：通过加载XML的方式
		FXMLLoader fx = new FXMLLoader();
		URL url=fx.getClassLoader().getResource("com/study/d019fxml/xmls/hello.fxml");
		AnchorPane anchorPane =(AnchorPane)FXMLLoader.load(url);

		//获取button
		Button b1=(Button)anchorPane.lookup("#clickBtn");



		stage.setScene(new Scene(anchorPane, 300, 400));
		stage.show();

		//点击事件
		b1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				System.out.println(event.getSource());
			}
		});


	}

}
